package com.wangyk.pay.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * @ClassName MapAndXml
 * @Description TODO(XML和Map格式互换)
 * @author wangyk
 * @Date 2018年9月18日 下午5:09:04
 * @version 1.0.0
 */
public class MapAndXml
{
    /**
     * @Description (Map转Xml格式)
     * @author wangyk
     * @param param
     * @return
     */
    public static String GetMapToXML(Map<String, String> param)
    {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        for (Map.Entry<String, String> entry : param.entrySet())
        {
            sb.append("<" + entry.getKey() + ">");
            sb.append(entry.getValue());
            sb.append("</" + entry.getKey() + ">");
        }
        sb.append("</xml>");
        return sb.toString();
    }
    
    /**
     * @Description (XML转Map格式)
     * @author wangyk
     * @param strxml
     * @return
     * @throws Exception
     */
    public static Map<String, String> doXMLParse(String strxml)
        throws Exception
    {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
        if (null == strxml || "".equals(strxml))
        {
            return null;
        }
        
        Map<String, String> m = new HashMap<String, String>();
        InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List<?> list = root.getChildren();
        Iterator<?> it = list.iterator();
        while (it.hasNext())
        {
            Element e = (Element)it.next();
            String k = e.getName();
            String v = "";
            List<?> children = e.getChildren();
            if (children.isEmpty())
            {
                v = e.getTextNormalize();
            }
            else
            {
                v = getChildrenText(children);
            }
            m.put(k, v);
        }
        // 关闭流
        in.close();
        return m;
    }
    
    public static String getChildrenText(List<?> children)
    {
        StringBuffer sb = new StringBuffer();
        if (!children.isEmpty())
        {
            Iterator<?> it = children.iterator();
            while (it.hasNext())
            {
                Element e = (Element)it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List<?> list = e.getChildren();
                sb.append("<" + name + ">");
                if (!list.isEmpty())
                {
                    sb.append(getChildrenText(list));
                }
                sb.append(value);
                sb.append("</" + name + ">");
            }
        }
        return sb.toString();
    }
}
